package com.example.nccdeploytool.controller;

import com.example.nccdeploytool.util.ConfigLoader;
import com.example.nccdeploytool.util.SFTPUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.control.cell.MapValueFactory;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;

import java.io.InputStream;
import java.net.URL;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

/**
 * ssh 登录
 *
 * @author 15037
 * @description
 * @date 2025-07-16 14:50
 */
public class SshCmdController implements Initializable {

    @FXML
    TableView tableSSH;
    @FXML
    Button btnRefresh;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        refresh();

        btnRefresh.setOnMouseClicked(e -> {
            refresh();
        });
    }

    public void refresh() {
        ObservableList columns = tableSSH.getColumns();
        tableSSH.getColumns().clear();
        tableSSH.refresh();

        List<Map<String, Object>> maps = ConfigLoader.loadJsonArr("ssh-cmd.json");

        // region 添加列
        if (maps != null && maps.size() > 0) {
            Map<String, Object> stringObjectMap = maps.get(0);
            for (String key : stringObjectMap.keySet()) {
                TableColumn column = new TableColumn(key);
                column.setCellValueFactory(new MapValueFactory<>(key));
                tableSSH.getColumns().addAll(column);
            }
        }

        // 创建按钮列
        TableColumn actionCol = new TableColumn<>("操作");

        // 设置单元格工厂
        actionCol.setCellFactory(param -> new TableCell<Map<String, Object>, String>() {
            @Override
            protected void updateItem(String o, boolean b) {
                super.updateItem(o, b);
                if (b) {
                    setGraphic(null);
                } else {
                    setGraphic(box);
                }
            }

            private final HBox box = new HBox();
            private final Button button = new Button("运行");
            private final Button btnFileList = new Button("文件列表");

            {
                box.getChildren().addAll(button, btnFileList);
                int index = getIndex();

                // 按钮点击事件
                button.setOnAction(e -> btnRunCmd(e, index));
            }

        });

        tableSSH.getColumns().add(actionCol);
        // endregion


        ObservableList<Object> result = FXCollections.observableArrayList(maps);

        tableSSH.setItems(result);
    }

    public void btnRunCmd(ActionEvent actionEvent, int index) {
        TableView<Map<String, Object>> tableView = this.tableSSH;

        Map<String, Object> person = tableView.getItems().get(index);
        System.out.println("编辑: " + person.get("cmd"));

        String ip = person.get("ip").toString();
        Integer port = Integer.parseInt(person.get("port").toString());
        String user = person.get("user").toString();
        String pwd = person.get("pwd").toString();
        String cmd = person.get("cmd").toString();


        Session session = null;
        try {
            session = SFTPUtil.sshRemoteCallLogin(
                    ip, port, user, pwd
            );
            String s = SFTPUtil.execCommandShell(cmd, session);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null) {
                session.disconnect();
            }
        }


    }


    public void cleanup() {

    }
}
